<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head lang="en">
    <meta charset="UTF-8">
    <title>智慧书坊</title>
    <link rel="stylesheet" type="text/css" th:href="@{/dist/css/bootstrap.min.css}">
    <link rel="stylesheet" type="text/css" th:href="@{/css/index.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/css/animate.css}"/>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}">
</head>

<body>
<!--导航部分  begin-->
<div th:replace="common/header :: nav"></div>
<!--导航部分 end-->


<div class="container">
    <ol class="breadcrumb">
        <li><a href="#" class="praise">个人信息修改</a></li>
    </ol>
</div>

<div class="container">
<!--个人信息begin-->
    <div>
        <form class="layui-form" action="" id="userInfo">
            <div class="layui-form-item" pane>
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-block">
                    <input type="text" name="username" required  lay-verify="required" autocomplete="off" class="layui-input" th:value="${user.username}" readonly="readonly" th:onclick="noChange()">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-block">
                    <input type="text" name="email" id="email" required  lay-verify="required" placeholder="请输入标题" th:value="${user.email}" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 90px">电话号码</label>
                <div class="layui-input-block">
                    <input type="text" name="phone" id="phone" required  lay-verify="required" placeholder="请输入标题" th:value="${user.phone}" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">学校</label>
                <div class="layui-input-block">
                    <input type="text" name="school" id="school" required  lay-verify="required" placeholder="请输入标题" th:value="${user.school}" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="formDemo" type="button" th:onclick="infoChange()" >提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    <button type="reset" class="layui-btn layui-btn-danger" th:onclick="pwdChange()">密码修改 >></button>
                </div>
            </div>
        </form>
    </div>

<!--个人信息end-->
    <hr>
    <!--首页底部信息 begin-->
    <div th:replace="common/footer :: footer"></div>
</div>
<!--首页底部信息end-->
<!--图书内容部分end-->

<!-- 登录模态框（Modal） -->
<!-- 注册modal end/.modal -->
<div th:replace="common/bookModal :: bookModal"></div>

<script src="dist/js/jquery.min.js" th:src="@{/dist/js/jquery.min.js}"></script>
<script src="js/carousel.js" th:src="@{/js/carousel.js}" type="text/javascript" charset="utf-8"></script>
<script src="dist/js/bootstrap.min.js" th:src="@{/dist/js/bootstrap.min.js}"></script>
<script src="js/wow.js" th:src="@{/js/wow.js}"></script>
<script th:src="@{/js/login_reg.js}"></script>
<script th:src="@{/layui/layui.js}"></script>

<script th:inline="javascript">
    //获取应用路径
    var contextPath = [[${#request.getContextPath()}]];
</script>
<script>
/*    //Demo
    layui.use('form', function(){
        var form = layui.form;

        //监听提交
        form.on('submit(formDemo)', function(data){
            layer.msg(JSON.stringify(data.field));
            return false;
        });
    });*/
</script>
<!-- 引入 layui.js 的 <script> 标签最好放置在 html 末尾 -->
<script type="text/javascript">
    if (!(/msie [6|7|8|9]/i.test(navigator.userAgent))) {
        new WOW().init();
    }

    //根据图书名搜索
    function searchBookName() {
        var inputBookName = $("#inputBookName").val();
        if (inputBookName != ''){
            window.location.href = contextPath + "/book/searchBook?inputBookName=" + inputBookName;
        }else alert('查找内容不为空')
    }

    function noChange() {
        layer.alert('用户名不可修改！', {icon: 2,anim:6});
    }

    //个人信息修改
    function infoChange() {
        var userInfo = $("#userInfo").serialize();
        var email = $("#email").val();
        var phone = $("#phone").val();
        var school = $("#school").val();
        if (email==''||phone== ''||school==''){
            layer.msg('必填项不为空', {icon: 7,anim:6});
            return;
        }
        var telReg = new RegExp("((\\d{11})|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)");
        if (!telReg.test(phone)){
            layer.msg('电话号码不合法！', {icon: 7,anim:6});
            return;
        }
        $.ajax({
            url: contextPath + "/user/checkEmail",
            data: {"email":email},
            method: "post",
            success: function (data) {
                if (data == 101) {//邮箱不存在
                    layer.confirm('确定修改？', {
                        icon:3,
                        btn: ['确定', '取消']
                    }, function(index, layero){
                        //按钮【按钮一】的回调
                        $.ajax({
                            url: contextPath + "/user/userInfoChange",
                            method: "post",
                            data: userInfo,
                            success: function (data) {
                                if (data == 'success') {
                                    layer.msg('修改成功');
                                } else layer.msg('修改失败')
                            }
                        })
                    }, function(index){
                        return ;
                    });

                } else {
                    layer.msg('邮箱已被注册', {icon: 7,anim:6});
                    return;
                }
            }
        })


    }
    //跳转至修改密码页面
    function pwdChange() {
        window.location.href = contextPath+"/user/toChangePwd"
    }
    //禁止a标签点击
    $(".praise").attr("disabled",true).css("pointer-events","none");
</script>
</body>

</html>